Import diddy
Import gameglobals

Class TitleScreen Extends Screen
	Field menu:SimpleMenu
	Field musicFormat:String
	Field backgroundImage:Image
	
	Method New()
		name = "title"
		backgroundImage = Backdrops.backdrop[MainGame.gdata[genTitle]].img'LoadImage(diddyGame.images.path + "Forest.png")
	End
	
	Method Start:Void()

		#rem
		menu = New SimpleMenu("ButtonOver", "ButtonClick", 0, 0, 10, False)
		Local b:SimpleButton = menu.AddButton("newgame.png", "newgameMO.png")
		b = menu.AddButton("continue.png", "continueMO.png")
		b = menu.AddButton("options.png", "optionsMO.png")
		b = menu.AddButton("quit.png", "quitMO.png")
		#end
		Print "Loading music: "+StripExt(Songs.music[MainGame.gdata[genTitleMus]].filename)+"."+TargetMusic
		
		diddyGame.MusicPlay(StripExt(Songs.music[MainGame.gdata[genTitleMus]].filename)+"."+TargetMusic, True)
		'menu.Centre()
	End
	
	Method Render:Void()
		Cls
		
		
		DrawImage backgroundImage, 0, 0
		'DrawImage MainLogo[0],160,32
		'SetColor(0,0,0)
		'SetAlpha(0.5)
		'DrawRect(160,120,320,240)
		SetColor(255,255,255)
		SetAlpha(1)		
		'DrawText "OHRPPGCE 2", SCREEN_WIDTH2, 10, 0.5, 0.5
	End
	
	Method ExtraRender:Void()
		'menu.Draw()
	End
	
	Method Update:Void()
		#rem
		menu.Update()
		
		If menu.Clicked("newgame") Then
			FadeToScreen(mainedscreen)
		End
		
		If menu.Clicked("options") Then
			FadeToScreen(optionScreen)
		End
		
		If menu.Clicked("continue") Then
			FadeToScreen(mainedscreen)
		End
		#end
		If KeyHit(KEY_ENTER)
		
			'show game saves and new game option.
		
			'Trigger new game script here
		
			loadScreen = New LoadingScreen(CURRENTMAP)
			FadeToScreen(loadScreen)'diddyGame.exitScreen)
		End
		
	End
End


Class LoadingScreen Extends Screen
	Field menu:SimpleMenu
	Field musicFormat:String
	Field backgroundImage:Image
	Field mapload:Int = 0
	Field mycounter:Int
	Field destination:Int
	
	Method New(mapnum:Int=0,dest:Int=-1)
		name = "title"
		backgroundImage = LoadImage(diddyGame.images.path + "loading.png")
		mapload = mapnum
		destination = dest
	End
	
	Method Start:Void()

		#rem
		menu = New SimpleMenu("ButtonOver", "ButtonClick", 0, 0, 10, False)
		Local b:SimpleButton = menu.AddButton("newgame.png", "newgameMO.png")
		b = menu.AddButton("continue.png", "continueMO.png")
		b = menu.AddButton("options.png", "optionsMO.png")
		b = menu.AddButton("quit.png", "quitMO.png")
		#end
		CURRENTMAP = mapload
		If Tilemaps[CURRENTMAP] = Null Then
			Local reader:MyTiledTileMapReader = New MyTiledTileMapReader
			Print "Loading: "+GMap[CURRENTMAP].Mapfile
			Local tmm:TileMap = reader.LoadMap(GMap[CURRENTMAP].Mapfile)
			Tilemaps[CURRENTMAP] = HTileMap(tmm)	
			Tilemaps[CURRENTMAP].HideColLayers()
			Tilemaps[CURRENTMAP].SetDoors(GMap[CURRENTMAP].doors,GMap[CURRENTMAP].links)	
			Tilemaps[CURRENTMAP].LoadNPCS(GMap[CURRENTMAP].npcs)
			Tilemaps[CURRENTMAP].entrydoorcol = true
			MainPlayer.map = Tilemaps[CURRENTMAP]
			If destination <> -1 Then
				CURRENTMAPX = (Tilemaps[CURRENTMAP].doors[destination].x*Tilemaps[CURRENTMAP].tileWidth)-SCREEN_WIDTH2
				CURRENTMAPY = ((Tilemaps[CURRENTMAP].doors[destination].y-1)*Tilemaps[CURRENTMAP].tileHeight)-SCREEN_HEIGHT2
				'do sprite midhandle
				'CURRENTMAPX = CURRENTMAPX + (MainPlayer.image.w/2)
				'CURRENTMAPY = CURRENTMAPY - (MainPlayer.image.h/2)
			Endif
		Else
			Tilemaps[CURRENTMAP].entrydoorcol = true
			MainPlayer.map = Tilemaps[CURRENTMAP]
			If destination <> -1 Then
				CURRENTMAPX = (Tilemaps[CURRENTMAP].doors[destination].x*Tilemaps[CURRENTMAP].tileWidth)-SCREEN_WIDTH2
				CURRENTMAPY = ((Tilemaps[CURRENTMAP].doors[destination].y-1)*Tilemaps[CURRENTMAP].tileHeight)-SCREEN_HEIGHT2
				'CURRENTMAPX = CURRENTMAPX + (MainPlayer.image.w/2)
				'CURRENTMAPY = CURRENTMAPY - (MainPlayer.image.h/2)				
			Endif				
			
		End If		
		musicFormat="ogg"

		'menu.Centre()
	End
	
	Method Render:Void()
		Cls
		DrawImage backgroundImage, 0, 0
		'DrawImage MainLogo[0],160,32
		SetColor(0,0,0)
		SetAlpha(0.5)
		'DrawRect(160,120,320,240)
		SetColor(255,255,255)
		SetAlpha(1)		
		'DrawText "OHRPPGCE 2", SCREEN_WIDTH2, 10, 0.5, 0.5
	End
	
	Method ExtraRender:Void()
		'menu.Draw()
	End
	
	Method Update:Void()
		mycounter = mycounter + 1
		
		
		If mycounter = 120 Then
			gameScreen.haltcontrol = false
			FadeToScreen(gameScreen)
		Endif
		#rem
		menu.Update()
		
		If menu.Clicked("newgame") Then
			FadeToScreen(mainedscreen)
		End
		
		If menu.Clicked("options") Then
			FadeToScreen(optionScreen)
		End
		
		If menu.Clicked("continue") Then
			FadeToScreen(mainedscreen)
		End
		
		If KeyHit(KEY_ESCAPE) Or menu.Clicked("quit")
			FadeToScreen(diddyGame.exitScreen)
		End
		#end
	End
End

Class MapScreen Extends Screen
	Field menu:SimpleMenu
	Field musicFormat:String
	Field backgroundImage:Image
	Field readComplete:Bool = False
	Field haltcontrol:Bool = False
	
	Method New()
		name = "title"
		'backgroundImage = LoadImage(diddyGame.images.path + "Forest.png")
		
		'majority of the game logic will be handled on this screen
		
	End
	
	Method Start:Void()

		#rem
		menu = New SimpleMenu("ButtonOver", "ButtonClick", 0, 0, 10, False)
		Local b:SimpleButton = menu.AddButton("newgame.png", "newgameMO.png")
		b = menu.AddButton("continue.png", "continueMO.png")
		b = menu.AddButton("options.png", "optionsMO.png")
		b = menu.AddButton("quit.png", "quitMO.png")
		#end
		'If Tilemaps[CURRENTMAP] = Null Then
			Local reader:MyTiledTileMapReader = New MyTiledTileMapReader
			Print "Loading: "+GMap[CURRENTMAP].Mapfile
			Local tmm:TileMap = reader.LoadMap(GMap[CURRENTMAP].Mapfile)
			Tilemaps[CURRENTMAP] = HTileMap(tmm)	
			Tilemaps[CURRENTMAP].HideColLayers()
			Tilemaps[CURRENTMAP].SetDoors(GMap[CURRENTMAP].doors,GMap[CURRENTMAP].links)	
			Tilemaps[CURRENTMAP].LoadNPCS(GMap[CURRENTMAP].npcs)
			Tilemaps[CURRENTMAP].entrydoorcol = true
			MainPlayer.map = Tilemaps[CURRENTMAP]
			'If destination <> -1 Then
			'	CURRENTMAPX = (Tilemaps[CURRENTMAP].doors[destination].x*Tilemaps[CURRENTMAP].tileWidth)-SCREEN_WIDTH2
			'	CURRENTMAPY = ((Tilemaps[CURRENTMAP].doors[destination].y-1)*Tilemaps[CURRENTMAP].tileHeight)-SCREEN_HEIGHT2
				'do sprite midhandle
				'CURRENTMAPX = CURRENTMAPX + (MainPlayer.image.w/2)
				'CURRENTMAPY = CURRENTMAPY - (MainPlayer.image.h/2)
			'Endif
		'endif		
		musicFormat="ogg"
		diddyGame.MusicPlay("day1."+musicFormat, True)
		'menu.Centre()
	End
	
	Method Render:Void()
		If Not readComplete
			diddyGame.images.ReadPixelsArray()
			readComplete = True
		End	
		Cls
		'DrawImage backgroundImage, 0, 0
		'DrawImage MainLogo[0],160,32
		Tilemaps[CURRENTMAP].RenderMap(CURRENTMAPX, CURRENTMAPY, SCREEN_WIDTH, SCREEN_HEIGHT)
		Tilemaps[CURRENTMAP].RenderNPCS(CURRENTMAPX, CURRENTMAPY, SCREEN_WIDTH, SCREEN_HEIGHT)
		Tilemaps[CURRENTMAP].RenderDoors(CURRENTMAPX, CURRENTMAPY, SCREEN_WIDTH, SCREEN_HEIGHT)
		'SetColor(0,0,0)
		'SetAlpha(0.5)
		'DrawRect(160,120,320,240)
		MainPlayer.Draw()
		SetColor(255,255,255)
		SetAlpha(1)		
		'DrawText "OHRPPGCE 2", SCREEN_WIDTH2, 10, 0.5, 0.5
	End
	
	Method ExtraRender:Void()
		'menu.Draw()
		'Tilemaps[CURRENTMAP].RenderNPCS(CURRENTMAPX, CURRENTMAPY, SCREEN_WIDTH, SCREEN_HEIGHT)
	End
	
	Method Update:Void()
		If haltcontrol = false
			MainPlayer.UpdateAnimation()
			MainPlayer.Update()
			
			'handledoor collision at this level
			If MainPlayer.doorcol = True Then			
				haltcontrol = True
				loadScreen = New LoadingScreen(Tilemaps[CURRENTMAP].links[MainPlayer.door].dest_map,Tilemaps[CURRENTMAP].links[MainPlayer.door].dest)
				MainPlayer.doorcol = false
				FadeToScreen(loadScreen)'diddyGame.exitScreen)
								
			endif
		endif
		#rem
		menu.Update()
		
		If menu.Clicked("newgame") Then
			FadeToScreen(mainedscreen)
		End
		
		If menu.Clicked("options") Then
			FadeToScreen(optionScreen)
		End
		
		If menu.Clicked("continue") Then
			FadeToScreen(mainedscreen)
		End
		
		If KeyHit(KEY_ESCAPE) Or menu.Clicked("quit")
			FadeToScreen(diddyGame.exitScreen)
		End
		#end
	End
End